

#ifndef   _UCED_H
#define   _UCED_H

#include "zadesk.h"
#include "zrxnames.h"
#include "zrxevent.h"

#pragma pack (push, 8)

struct IDispatch;
#define UCED_SERVICES  UCRX_T("UcEdServices")
#define UCED_EDITOR_OBJ UCRX_T("UcEditor")

enum UcadContextMenuMode {
    kDefault = 0,           
    kEdit,                  
    kCommand,               
    kHotGrip,               
    kOsnap,
#ifndef _ZSOFT_WINDOWS_
	kCMPaperSpace
#endif 
};

enum xrefSubcommandActivities
{    
    kStart = 0,
    kStartItem = 2,
    kEndItem = 3,
    kEnd = 4,
    kWillAbort = 5,
    kAborted = 6,
    kStartXBindBlock = 7,
    kStartXBindSymbol = 8
};

enum undoSubcommandActivities
{
    kNone = 0,
    kOne = 1,
    kAll = 2
};

#pragma pack (pop)
#include "zaccmd.h"
#pragma pack (push, 8)

#if defined(__cplusplus)

struct UcEdCommandStruc;
class UcString;

extern "C" {
#endif

int ucedCmdLookup(const ZTCHAR* cmdStr, int globalLookup,
                  UcEdCommandStruc* retStruc,
                  int skipUndef = FALSE);

int ucedCmdLookup2(const ZTCHAR* cmdStr, int globalLookup,
                   UcEdCommandStruc* retStruc,
                   int sf = UCRX_CMD_DEFUN);

int ucedCmdUndefine(const ZTCHAR* cmdStr, int undefIt);

int ucedPopCommandDirectory(const ZTCHAR* cmdDirName);

class UcApDocument;
Ucad::ErrorStatus ucedGetCommandForDocument(UcApDocument* pDoc,
                                            ZTCHAR*& pGlobalCmdName);

ZSoft::Boolean ucedSetOLELock(int handle, int flags=0);

ZSoft::Boolean ucedClearOLELock(int handle);

void ucedPostCommandPrompt();
 
#if defined(__cplusplus)

}

Ucad::ErrorStatus ucedSyncFileOpen(const ZTCHAR *pszFileToOpen,
                                   const wchar_t* wszPassword = NULL);

ZSoft::Boolean ucedSetColorDialog(int& nColor, ZSoft::Boolean bAllowMetaColor,
    int nCurLayerColor);

ZSoft::Boolean ucedSetColorDialogTrueColor( UcCmColor& color, ZSoft::Boolean bAllowMetaColor,
                                           const UcCmColor& curLayerColor, UcCm::DialogTabs tabs = (UcCm::DialogTabs)(UcCm::kACITab | UcCm::kTrueColorTab  | UcCm::kColorBookTab) );
#ifdef _ZSOFT_WINDOWS_
typedef void (WINAPI* UcEdColorDialogCallbackFunction)(void *pCallbackData, const UcCmColor& newColor);
ZSoft::Boolean ucedSetColorDialogTrueColorWithCallback( UcCmColor& color,
                                                        ZSoft::Boolean bAllowMetaColor,
                                                        const UcCmColor& curLayerColor,
                                                        UcCm::DialogTabs tabs = (UcCm::DialogTabs)(UcCm::kACITab | UcCm::kTrueColorTab  | UcCm::kColorBookTab),
                                                        UcEdColorDialogCallbackFunction callback = NULL,
														void * pCallbackData = NULL);
#endif

ZSoft::Boolean ucedSetColorPrompt( ZTCHAR* promptString,
                                   UcCmColor& color,
                                   ZSoft::Boolean bAllowMetaColor);

ZSoft::Boolean ucedIsMenuGroupLoaded(const ZTCHAR *pszGroupName);

void ucedSendModelessOperationStart(const ZTCHAR* strContext);
void ucedSendModelessOperationEnded(const ZTCHAR* strContext);

void ucedUpdateDisplay();

void ucedUpdateDisplayPause (bool bEnable);

bool ucedIsUpdateDisplayPaused (void);

ZSoft::Boolean ucedInitDialog(ZSoft::Boolean useDialog = ZSoft::kTrue);

void
ucedDisableDefaultZRXExceptionHandler(ZSoft::Boolean disable = ZSoft::kTrue);

Ucad::ErrorStatus ucedVportTableRecords2Vports();

Ucad::ErrorStatus ucedVports2VportTableRecords();

Ucad::ErrorStatus
ucedVPLayer(const UcDbObjectId& vpId, const UcDbObjectIdArray& layerIds,
    UcDb::VpFreezeOps operation);

void ucedRegenLayers(const UcDbObjectIdArray& layerIds, int nParam1, int nParam2);

#define ucedServices  \
UcEdServices::cast(ucrxServiceDictionary->at(UCED_SERVICES))

class UcEdServices: public UcRxService 
{ 
public:
    UCRX_DECLARE_MEMBERS(UcEdServices);
    virtual UcEdCommand* newUcEdCommand(UcRxFunctionPtr cmdFcnAddr,
        ZSoft::Int32 cmdFlags) const = 0;
};

class UcDbDatabase;

class UcEditorReactor: public UcRxEventReactor 
{ 
public:
    UCRX_DECLARE_MEMBERS(UcEditorReactor);
    UcEditorReactor() : mVeto(0) {}

    virtual void unknownCommand  (const ZTCHAR* cmdStr, UcDbVoidPtrArray *al);
    virtual void commandWillStart(const ZTCHAR* cmdStr);
    virtual void commandEnded    (const ZTCHAR* cmdStr);
    virtual void commandCancelled(const ZTCHAR* cmdStr);
    virtual void commandFailed   (const ZTCHAR* cmdStr);

    virtual void lispWillStart   (const ZTCHAR* firstLine);
    virtual void lispEnded       ();
    virtual void lispCancelled   ();

    ZSOFT_DEPRECATED virtual void beginClose(UcDbDatabase* pDwg);

    virtual void beginDxfIn      (UcDbDatabase*);
    virtual void abortDxfIn      (UcDbDatabase*);
    virtual void dxfInComplete   (UcDbDatabase*);
    
    virtual void beginDxfOut     (UcDbDatabase*);
    virtual void abortDxfOut     (UcDbDatabase*);
    virtual void dxfOutComplete  (UcDbDatabase*);

    virtual void beginDwgOpen    (ZTCHAR* filename);
    virtual void endDwgOpen      (const ZTCHAR* filename, UcDbDatabase* pDb);
    virtual void initialDwgFileOpenComplete(UcDbDatabase* pDwg);

    virtual void dwgFileOpened(UcDbDatabase*, ZTCHAR* fileName);
    virtual void databaseConstructed(UcDbDatabase*);
    virtual void databaseToBeDestroyed(UcDbDatabase*);
    virtual void beginSave    (UcDbDatabase*, const ZTCHAR* pIntendedName);
    virtual void saveComplete (UcDbDatabase*, const ZTCHAR* pActualName);
    virtual void abortSave    (UcDbDatabase*);

    virtual void beginInsert(UcDbDatabase* pTo, const ZTCHAR* pBlockName,
                             UcDbDatabase* pFrom);
    virtual void beginInsert(UcDbDatabase* pTo, const UcGeMatrix3d& xform,
                             UcDbDatabase* pFrom);
    virtual void otherInsert(UcDbDatabase* pTo, UcDbIdMapping& idMap,
                             UcDbDatabase* pFrom);
    virtual void abortInsert(UcDbDatabase* pTo);
    virtual void endInsert  (UcDbDatabase* pTo);

    virtual void wblockNotice(UcDbDatabase* pDb);
    virtual void beginWblock(UcDbDatabase* pTo, UcDbDatabase* pFrom,
                             const UcGePoint3d*& insertionPoint);
    virtual void beginWblock(UcDbDatabase* pTo, UcDbDatabase* pFrom,
                             UcDbObjectId blockId);
    virtual void beginWblock(UcDbDatabase* pTo, UcDbDatabase* pFrom);
    virtual void otherWblock(UcDbDatabase* pTo, UcDbIdMapping&,
                             UcDbDatabase* pFrom);
    virtual void abortWblock(UcDbDatabase* pTo);
    virtual void endWblock  (UcDbDatabase* pTo);

    virtual void beginDeepClone       (UcDbDatabase* pTo, UcDbIdMapping&);
    virtual void beginDeepCloneXlation(UcDbIdMapping&, Ucad::ErrorStatus*);
    virtual void abortDeepClone       (UcDbIdMapping&);
    virtual void endDeepClone         (UcDbIdMapping&);

    virtual void sysVarChanged        (const ZTCHAR* varName, ZSoft::Boolean success);
    virtual void sysVarWillChange     (const ZTCHAR* varName);

    virtual void beginAttach (UcDbDatabase* pTo, const ZTCHAR*,
                              UcDbDatabase* pFrom);
    virtual void otherAttach (UcDbDatabase* pTo, UcDbDatabase* pFrom);
    virtual void abortAttach (UcDbDatabase* pFrom);
    virtual void endAttach   (UcDbDatabase* pTo);
    virtual void redirected  (UcDbObjectId  newId, UcDbObjectId oldId);
    virtual void comandeered (UcDbDatabase* pTo, UcDbObjectId id,
                              UcDbDatabase* pFrom);
    virtual void beginRestore(UcDbDatabase* pTo, const ZTCHAR*,
                              UcDbDatabase* pFrom);
    virtual void abortRestore(UcDbDatabase* pTo);
    virtual void endRestore  (UcDbDatabase* pTo);

    virtual void xrefSubcommandBindItem   (UcDbDatabase* pHost, int activity, UcDbObjectId blockId);
    virtual void xrefSubcommandAttachItem (UcDbDatabase* pHost, int activity, const ZTCHAR* pPath);
    virtual void xrefSubcommandOverlayItem(UcDbDatabase* pHost, int activity, const ZTCHAR* pPath);
    virtual void xrefSubcommandDetachItem (UcDbDatabase* pHost, int activity, UcDbObjectId blockId);
    virtual void xrefSubcommandPathItem   (int activity, UcDbObjectId blockId,
                                           const ZTCHAR* pNewPath);
    virtual void xrefSubcommandReloadItem (UcDbDatabase* pHost, int activity, UcDbObjectId blockId);
    virtual void xrefSubcommandUnloadItem (UcDbDatabase* pHost, int activity, UcDbObjectId blockId);

    virtual void undoSubcommandAuto       (int activity, ZSoft::Boolean state);
    virtual void undoSubcommandControl    (int activity, int option);
    virtual void undoSubcommandBegin      (int activity);
    virtual void undoSubcommandEnd        (int activity);
    virtual void undoSubcommandMark       (int activity);
    virtual void undoSubcommandBack       (int activity);
    virtual void undoSubcommandNumber     (int activity, int num);

    virtual void pickfirstModified();
    virtual void layoutSwitched(const ZTCHAR* newLayoutName);

    virtual void docFrameMovedOrResized(ZSoft::LongPtr hwndDocFrame, bool bMoved);
    virtual void mainFrameMovedOrResized(ZSoft::LongPtr hwndMainFrame, bool bMoved);

    virtual void beginDoubleClick(const UcGePoint3d& clickPoint);
    virtual void beginRightClick(const UcGePoint3d& clickPoint);

    virtual void toolbarBitmapSizeWillChange(ZSoft::Boolean bLarge);
    virtual void toolbarBitmapSizeChanged(ZSoft::Boolean bLarge);

    virtual void beginWblockObjects (UcDbDatabase* pFrom, UcDbIdMapping& );

    virtual void partialOpenNotice(UcDbDatabase* pDb);
    virtual void objectsLazyLoaded(const UcDbObjectIdArray& idArray);

    virtual void beginQuit();
    virtual void quitAborted();
    virtual void quitWillStart();

    virtual void modelessOperationWillStart(const ZTCHAR* contextStr);
    virtual void modelessOperationEnded(const ZTCHAR* contextStr);

    virtual void cmdIUnkModified(const ZTCHAR* strCommand);
    virtual Ucad::ErrorStatus xrefSubCommandStart(
            UcXrefSubCommand subcmd,
            const UcDbObjectIdArray& btrIds,
            const ZTCHAR* const * btrNames,
            const ZTCHAR* const * paths);
    virtual void preXrefLockFile(UcDbObjectId btrId);

    virtual void viewChanged();

protected:
    Ucad::ErrorStatus veto();

private:
    ZSoft::UInt8 mVeto;

friend class UcEditorImp;
};

class UcEditorReactor2 : public UcEditorReactor
{
public:
    UCRX_DECLARE_MEMBERS(UcEditorReactor2);

    virtual void beginDocClose(UcDbDatabase* pDwg) {}

    virtual void docCloseAborted(UcDbDatabase* pDwg) {}

    virtual void docCloseWillStart(UcDbDatabase* pDwg) {}

    virtual void layoutToBeSwitched (const ZTCHAR *oldLayoutName, const ZTCHAR *newLayoutName) {}

    virtual void dwgViewResized (ZSoft::LongPtr hwndDwgView) {}

};

#define ucedEditor UcEditor::cast(ucrxSysRegistry()->at(UCED_EDITOR_OBJ))

class UcEditor: public UcRxEvent 
{ 
public:
    UCRX_DECLARE_MEMBERS(UcEditor);
    virtual void addReactor   (UcRxEventReactor * newObj) = 0;
    virtual void removeReactor(UcRxEventReactor * delObj) = 0;
};

class UcEdUIContext : public UcRxObject
{
public:
    UCRX_DECLARE_MEMBERS(UcEdUIContext);
    virtual void* getMenuContext(const UcRxClass *, 
                                 const UcDbObjectIdArray&)=0;
    virtual void* getMenuContext(const UcRxClass*,
                                 const UcDbObjectIdArray&,
                                 const UcArray<UcDbFullSubentPathArray,
                                               UcArrayObjectCopyReallocator<UcDbFullSubentPathArray> >& subentIds);

    virtual void  onCommand(ZSoft::UInt32 ) =0;
    virtual void  OnUpdateMenu();
};

ZSoft::Boolean ucedAddObjectContextMenu(UcRxClass *pClass, 
                                        UcEdUIContext *pContext,
                                        const void *appId);
ZSoft::Boolean ucedRemoveObjectContextMenu(UcRxClass *pClass, 
                                           UcEdUIContext *pContext);

ZSoft::Boolean ucedAddDefaultContextMenu(UcEdUIContext *pContext, 
                                         const void *appId, 
                                         const ZTCHAR* appName=NULL);
ZSoft::Boolean ucedRemoveDefaultContextMenu(UcEdUIContext *pContext);

bool    ucedSetCMBaseAlias(LPCTSTR  strAlias, UcadContextMenuMode mode);
LPCTSTR ucedGetCMBaseAlias(UcadContextMenuMode mode);

Ucad::ErrorStatus ucedMspace();  
Ucad::ErrorStatus ucedPspace();  
Ucad::ErrorStatus ucedSetCurrentVPort(int vpnumber);
Ucad::ErrorStatus ucedSetCurrentVPort(const UcDbViewport* pVp);

int ucedIsDragging(void);

Ucad::ErrorStatus
ucedSetCurrentView(UcDbViewTableRecord *pVwRec, UcDbViewport *pVP);

Ucad::ErrorStatus ucedRestoreCurrentView(const UcDbObjectId& namedViewId);

Ucad::ErrorStatus ucedSetCurrentUCS(const UcGeMatrix3d & mat);
Ucad::ErrorStatus ucedGetCurrentUCS(UcGeMatrix3d& mat);
Ucad::ErrorStatus ucedRestorePreviousUCS();

class UcDbFcf;
class UcDbMText;

void ucedEditToleranceInteractive(UcDbFcf* pTol);

int ucedEditMTextInteractive(UcDbMText* pMText, bool useNewUI = false, bool allowTabs = false);

ZSoft::UInt32    ucedGetRGB(int color);

Ucad::ErrorStatus ucedGetCurrentSelectionSet(UcDbObjectIdArray& sset);

Ucad::ErrorStatus ucedGetFullSubentPathsForCurrentSelection(
        const UcDbObjectId&      selectedObject,
        UcDbFullSubentPathArray& selectedSubentities);

class UcDbHatch;
class UcDbEntity;

#define ucedGetIDispatch UecadGetIDispatch
extern "C" IDispatch *ucedGetIDispatch (BOOL bAddRef);

UcDbObjectId ucedActiveViewportId();

UcDbObjectId ucedViewportIdFromNumber(int nVpNum);

int ucedNumberOfViewports();

UcDbObjectId ucedGetCurViewportObjectId();

Ucad::ErrorStatus 
ucedConvertEntityToHatch(UcDbHatch* pHatch, UcDbEntity*& pEnt, bool transferId);

enum UcEdDrawOrderCmdType {
    kDrawOrderNone = 0,
    kDrawOrderBottom,
    kDrawOrderTop,
    kDrawOrderBelow,
    kDrawOrderAbove
};

Ucad::ErrorStatus  ucedDrawOrderInherit(UcDbObjectId parent,
                                        UcDbObjectIdArray &childArray,
                                        UcEdDrawOrderCmdType cmd);

Ucad::ErrorStatus
ucedCreateViewportByView(UcDbDatabase *pDb,
                         const UcDbObjectId& view, 
                         const UcGePoint2d& location, 
                         double scale,
                         UcDbObjectId& viewportId);

Ucad::ErrorStatus
ucedCreateEnhancedViewportOnDrop(const ZTCHAR *filename,
                                 const ZTCHAR *viewName,
                                 const UcGePoint2d location,
                                 double scale,
                                 UcDbObjectId labelBlockId,
                                 UcDbObjectId& sheetViewId,
                                 UcDbObjectId& viewportId);

Ucad::ErrorStatus
ucedCreateEnhancedViewportOnDropDWG(const ZTCHAR *filename,
                                    UcDbExtents *extents,
                                    const UcGePoint2d location,
                                    double scale,
                                    UcDbObjectId labelBlockId,
                                    UcDbObjectId& sheetViewId,
                                    UcDbObjectId& viewportId);

const UcString* ucedGetUnitsValueString (UcDb::UnitsValue units);

bool ucdbCanonicalToSystemRange(int eUnits, const UcString& strIn, UcString& strOut);

bool ucdbSystemRangeToCanonical(int eUnits, const UcString& strIn, UcString& strOut);

Ucad::ErrorStatus ucedTraceBoundary(const UcGePoint3d& seedPoint, bool detectIslands, UcDbVoidPtrArray& resultingBoundarySet);

enum BlockEditModeFlags {
    kBlkEditModeNone   = 0x0,
    kBlkEditModeActive = 0x01,
    kBlkEditModeOpen   = 0x02,
    kBlkEditModeDirty  = 0x04
};

unsigned int ucedGetBlockEditMode();

#endif 

#pragma pack (pop)
#include "zdbjig.h"

#endif
